<?php
class User {
	public function __construct() {
		$username = null;
		$id = null;
		$photo = null;
		$origin = null;
		$this->username = $username;
		$this->id = $id;
		$this->photo = $photo;
		$this->origin = $origin;
	}
	
	public function get_username() {
		return $this->username;
	}
	
	public function get_origin() {
		return $this->origin;
	}
	
	public function get_photo($profile_id) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$mysqli = new mysqli($host, $user, $password, $database);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
        }
		$sql = "SELECT user_photo_file FROM users WHERE user_id = '$profile_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows != 1) {
				return 2;
			} else {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					if($user_photo_file == null) {
						return "default-photo.jpg";
					} else {
						return "profile/$user_photo_file";
					}
				}
				return 1;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	public function get_information($profile_id) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$mysqli = new mysqli($host, $user, $password, $database);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
        }
		$sql = "SELECT username, region_id, user_photo_file FROM users WHERE user_id = '$profile_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows != 1) {
				return 2;
			} else {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					$this->username = $row['username'];
					$this->photo = $row['user_photo_file'];
					$regionID = $row['region_id'];
					$sql2 = "SELECT region_name, country_name FROM regions NATURAL LEFT JOIN countries WHERE region_id = '$regionID'";
					if($result2 = $mysqli->query($sql2)) {
						while($row2 = $result2->fetch_array(MYSQLI_ASSOC)) {
							$this->origin = $row2['region_name'] . ", " . $row2['country_name'];
						}
					}
				}
				return 1;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	public function login($username, $user_password) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$mysqli = new mysqli($host, $user, $password, $database);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
        }
		$username = $mysqli->real_escape_string($username);
		$user_password = $mysqli->real_escape_string($user_password);
		$user_password = crypt($user_password, '$1$lipgdwx2c');
		$sql = "SELECT user_id, username FROM users WHERE username = '$username' AND password = '$user_password'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows != 1) {
				return 2;
			} else {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					$_SESSION['auth'] = $row['user_id'];
					$_SESSION['username'] = $row['username'];
				}
				return 1;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	public function register_user($username, $password1) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$mysqli = new mysqli($host, $user, $password, $database);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
        }
		$username = $mysqli->real_escape_string($username);
		$password1 = $mysqli->real_escape_string($password1);
		$result_num_row = $this->checkUserExists($mysqli, $username);
		if($result_num_row != 0) {
			return 3;
		} else {
			$password = crypt($password1, '$1$lipgdwx2c');
			$sql = "INSERT INTO users(username, password, region_id, user_photo_file) VALUES('$username', '$password', null, null)";
			$mysqli->query($sql);
			return 4;
		}
		$result->close();
		$mysqli->close();	
	}
	public function checkUserExists($mysqli, $username) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$sql = "SELECT username FROM users WHERE username = '$username'";
		if($result = $mysqli->query($sql)) {
			$row_count = $result->num_rows;
		}
		return $row_count;
	}
	public function checkUsernameLength($username) {
		if(strlen($username) < 3 || strlen($username) > 16) {
			return 0;
		} else {
			return 1;
		}
	}
}
